#!/bin/bash

echo 安装必要依赖
sudo apt-get install -y libssl1.1=1.1.1d-0+deb10u6
sudo apt-get install -y python3
sudo apt-get install -y python3-dev
sudo apt-get install -y python3-venv
sudo apt-get install -y python3-pip
sudo apt-get install -y libffi-dev
sudo apt-get install -y libssl-dev
sudo apt-get install -y libjpeg-dev
sudo apt-get install -y zlib1g-dev
sudo apt-get install -y autoconf
sudo apt-get install -y build-essential
sudo apt-get install -y libopenjp2-7
sudo apt-get install -y libtiff5
sudo apt-get install -y build-essential
sudo apt-get install -y libsqlite3-dev
sudo apt-get install -y sqlite3
sudo apt-get install -y bzip2
sudo apt-get install -y libbz2-dev

echo 大陆python源
mkdir /root/.pip
cat >>~/.pip/pip.conf<<EOF
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF

echo 安装python3.8最新版
wget https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tgz
tar zxvf Python-3.8.10.tgz
cd Python-3.8.10
sudo ./configure
sudo make
sudo make install
echo python3.8安装成功

echo 安装python所需依赖
python3.8 -m pip install wheel

echo 安装hass本体
pip3.8 install homeassistant

echo hass安装成功

echo 创建hass服务
cat >>/lib/systemd/system/hass.service<<EOF
[Unit]
Description=hass
Wants=network.target

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/root

Nice=5
ExecStart=hass
KillSignal=SIGINT
Restart=on-failure
SyslogIdentifier=hass
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target
EOF

cat >>/etc/rsyslog.d/hass.conf<<EOF
if \$programname == 'hass' then /root/logs/hass.log
& stop
EOF

systemctl restart rsyslog
systemctl daemon-reload
systemctl enable hass

echo 服务创建成功
echo python源代码已经无用,可自行执行命令删除 rm -rf /root/Python-3.8.10*